// pages/index/index-detail/index-detail.js
var com = require("../../../com/com.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:"",
    id:null,
    yes:false,
    musicPlay:false
  },
  // 点击收藏
  sctap: function (e) {
    var newsCollect = wx.getStorageSync("newsCollect");
    var newCollect = newsCollect[this.data.id];
    newCollect = !newCollect;
    newsCollect[this.data.id] = newCollect;
    wx.setStorageSync("newsCollect", newsCollect)
    this.setData({
      yes: newCollect
    })
    // wx.clearStorage()
    wx.showToast({
      title:newCollect ? "收藏成功" : "取消收藏",
      icon:"success",
      duration:1000,
      mask:true
    })
  },

  // 播放音乐
  musicTap:function(e){
    console.log(this.data.musicPlay)
    if (this.data.musicPlay){
      wx.pauseBackgroundAudio();
    }else{
      wx.playBackgroundAudio({
        dataUrl: this.data.list.music.url,
        title: this.data.list.music.title,
        coverImgUrl: this.data.list.music.coveImg
      })
    }
    this.setData({
      musicPlay: !this.data.musicPlay
    })
    var that = this;
    wx.onBackgroundAudioPause(function () {
      that.setData({
        musicPlay: false
      })
    })
    wx.onBackgroundAudioPlay(function () {
      that.setData({
        musicPlay: true
      })
    })
    wx.onBackgroundAudioStop(function(){
      that.setData({
        musicPlay: false
      })
    })
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    


    this.setData({
      list: com.data[options.id],
      id: com.data[options.id].id
    })
    console.log(this.data.id,this.data.list)
    // 本地存储
    var newsCollect = wx.getStorageSync("newsCollect");
    if (newsCollect){
      var newCollect = newsCollect[options.id];
      if (newCollect){
        this.setData({
          yes: newCollect
        })
      }else{
        newsCollect[options.id] = false;
        wx.setStorageSync("newsCollect", newsCollect)
        this.setData({
          yes: newsCollect[options.id]
        })
      }
    }else{
      var newsCollect = {};
      newsCollect[options.id] = false;
      wx.setStorageSync("newsCollect", newsCollect)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log(111)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})